// pages/userinfo/userinfo.js

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:1,
    second:90,
    seconds:90
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  getPhone:function(e){
    var phone = e.detail.value
    this.setData({
      phone:phone
    })
    console.log(phone)
  },
  send:function(e){
    var that = this
    var second = this.data.second
    var phone = this.data.phone
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showModal({
        title: '提示',
        content: '手机号码有误，请重新收入',
        showCancel: false,
      })
      return 
    }
    wx.showLoading({
      title: '正在发送',
      mask: true,
    })
    that.setData({
      active: 0
    })
    wx.request({
      url: app.globalData.telurl + 'sendMsg?tel=' + phone+ '&openid=' + wx.getStorageSync('openId'),
      success:function(res){
        that.interval()
        that.setData({
          second: second,
        })
      },
      fail:function(){
        wx.showModal({
          title: '提示',
          content: '验证码发送失败，请重新发送',
          showCancel: true,
          success: function(res) {
            if(res.confirm){
              that.send()
            }
          }
        })
      },
      complete:function(){
        wx.hideLoading()
      }
    })

    
  },
  interval:function(){
    var that = this
    var second = this.data.second
    var interval = setInterval(function(){
      if (second > 1){
        second--
        that.setData({
          second: second,
          active:0
        })
      }else{
        clearInterval(interval)
        that.setData({
          second: 90,
          active: 1
        })
      }
    },1000)
    
  },
  getyanzhen:function(e){
    var yanzhen = e.detail.value
    this.setData({
      yanzhen: yanzhen
    })
  },
  summit:function () {
    wx.request({
      url: app.globalData.url + 'teladd?openid=' + wx.getStorageSync('openId') + '&tel=' + this.data.phone + '&yanzhen='+this.data.yanzhen,
      success:function(res){
        console.log(res)
        if(res.data  == '验证错误'){
          wx.showModal({
            title: '提示',
            content: '验证码错误，请重新输入',
            showCancel: false,
          })
        }else{
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 1500,
            mask: true,
            success: function(res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})